﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using ueEngine.Biz.Core.ExServer.Statistics;
using ueEngine.Framework.Common;

namespace ueEngine.Web.WebLogic
{
    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,AllowMultiple=false,Inherited=true)]
    public class TraceBehaviorAttribute : FilterAttribute,IActionFilter
    {
        int id = 0;
        #region IActionFilter 成员

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            try
            {
                if (System.Configuration.ConfigurationManager.AppSettings["enabletrace"] == "true")
                {
                    new BehaviorTrace().UpdateCompleteTimeForBehaviorTrace(id);
                }
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.WebError, LogLevel.Error, e);
            }
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                if (System.Configuration.ConfigurationManager.AppSettings["enabletrace"] == "true")
                {
                    string userid = string.Empty;
                    if (filterContext.HttpContext.User.Identity.IsAuthenticated)
                    {
                        userid = filterContext.HttpContext.User.Identity.Name;
                    }
                    else if (filterContext.HttpContext.Request.Cookies["user"] == null)
                    {
                        userid = Guid.NewGuid().ToString();
                        filterContext.HttpContext.Response.Cookies.Set(
                            new HttpCookie("user", userid));
                    }
                    else
                    {
                        userid = filterContext.HttpContext.Request.Cookies["user"].Value;
                    }

                    var request = filterContext.HttpContext.Request;
                    string clientip = request.ServerVariables["REMOTE_ADDR"].ToString();
                    string sessionId = userid;
                    string httpMethod = request.HttpMethod;
                    string requestPath = request.Path;
                    string browser = request.Browser.Type;

                    string exchangeData = string.Empty;
                    Dictionary<string, string> dict = new Dictionary<string, string>();
                    if (httpMethod.ToLower() == "post")
                    {
                        foreach (string key in request.Form.AllKeys)
                        {
                            dict.Add(key, request.Form[key]);
                        }
                    }
                    if (dict.Count > 0)
                    {
                        exchangeData = dict.ToJson();
                    }
                    new BehaviorTrace().RecordUserBehavior(
                        clientip, userid, httpMethod, requestPath, browser, exchangeData, out id);
                }
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.WebError, LogLevel.Error, e);
            }
        }
        #endregion
    }
}
